package com.atguigu.jedis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.ListPosition;

import java.util.List;

/**
 * ClassName: JedisDemo
 * Package: com.atguigu.jedis
 * Description:
 *
 * @Author: tubage
 * @Create: 2024/3/25 11:30
 * @Version: 1.0
 */
public class JedisDemo {
    private static String host = "localhost";
    private static int port = 16379;
    private static JedisPool jedisPool;
    static {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(10);
        jedisPoolConfig.setMaxIdle(5);
        jedisPoolConfig.setMinIdle(5);
        jedisPoolConfig.setBlockWhenExhausted(true);
        jedisPoolConfig.setMaxWaitMillis(2000);
        jedisPoolConfig.setTestOnBorrow(true);

        jedisPool = new JedisPool(jedisPoolConfig,host,port);
    }

    public static Jedis getJedisFromPool() {
        return jedisPool.getResource();
    }

    public static Jedis getJedis() {
        Jedis jedis = new Jedis(host, port);
        return jedis;
    }

    public static void closeJedis(Jedis jedis) {
        if(jedis != null) jedis.close();
    }
    public static void main(String[] args) {
        Jedis jedis = getJedisFromPool();
        String ping = jedis.ping();
        System.out.println(ping);

//        testString(jedis);
//        testList(jedis);
//        testSet(jedis);
//        testZset(jedis);
        testHash(jedis);

        jedis.flushDB();
        closeJedis(jedis);
    }

    public static void testString(Jedis jedis)   {
        jedis.set("k1", "v1");
        jedis.mset("k2", "v2","k3", "v3");
        jedis.append("k1","abcd");
        jedis.setex("k4", 10, "v4");
        jedis.set("email", "12345@qq.com");
        System.out.println("jedis.getrange(\"email\", 6, -1) = " + jedis.getrange("email", 6, -1));
        jedis.getSet("email", "12345@qq.com");
        List<String> mget = jedis.mget("k1", "k2", "k3", "k4", "email");
        System.out.println("mget = " + mget);
        System.out.println("jedis.strlen(\"k1\") = " + jedis.strlen("k1"));
    }

    public static void testList(Jedis jedis) {
        jedis.lpush("l1", "1","2","3","a","b","{'id':1001}");
        System.out.println("jedis.llen(\"l1\") = " + jedis.llen("l1"));
        System.out.println("jedis.lindex(\"l1\", 0) = " + jedis.lindex("l1", 0));
        jedis.linsert("l1", ListPosition.AFTER, "a", "4");
        System.out.println("jedis.lrange(\"l1\", 0 , -1) = " + jedis.lrange("l1", 0, -1));
    }

    public static void testSet(Jedis jedis) {
        jedis.sadd("s1", "1","2","3","4","5","a");
        jedis.sadd("s2", "a", "b");
        System.out.println("jedis.smembers(\"s1\") = " + jedis.smembers("s1"));
        System.out.println("jedis.scard(\"s1\") = " + jedis.scard("s1"));
        System.out.println(jedis.sismember("s1", "b"));
        System.out.println(jedis.srandmember("s1", 2));
        System.out.println("jedis.sdiff(\"s1\", \"s2\") = " + jedis.sdiff("s1", "s2"));
    }

    public static void testZset(Jedis jedis) {
        jedis.zadd("z1",100,"tom");
        jedis.zadd("z1",120,"tim");
        jedis.zadd("z1",80,"john");
        System.out.println("jedis.zrange(\"z1\", 0, -1) = " + jedis.zrange("z1", 0, -1));
        System.out.println("jedis.zcount(\"z1\", 100, 120) = " + jedis.zcount("z1", 100, 120));
        System.out.println("jedis.zrevrank(\"z1\", \"john\") = " + jedis.zrevrank("z1", "john"));
        System.out.println("jedis.zrevrangeByScoreWithScores(\"z1\", 120, 100) = " + jedis.zrevrangeByScoreWithScores("z1", 120, 100));
    }

    public static void testHash(Jedis jedis) {
        jedis.hset("user:1001", "id", "1001");
        jedis.hset("user:1001", "name", "zhangsan");
        jedis.hset("user:1001", "age", "20");
        System.out.println("jedis.hgetAll(\"user:1001\") = " + jedis.hgetAll("user:1001"));
        System.out.println("jedis.hkeys(\"user:1001\") = " + jedis.hkeys("user:1001"));
        System.out.println("jedis.hvals(\"user:1001\") = " + jedis.hvals("user:1001"));
        System.out.println("jedis.hexists(\"user:1001\", \"gender\") = " + jedis.hexists("user:1001", "gender"));
        System.out.println("jedis.hsetnx(\"user:1001\", \"name\", \"zhangsan111\" ) = " + jedis.hsetnx("user:1001", "name", "zhangsan111"));
        System.out.println("jedis.hincrBy(\"user:1001\", \"age\", 1) = " + jedis.hincrBy("user:1001", "age", 1));
        System.out.println("jedis.hget(\"user:1001\", \"age\") = " + jedis.hget("user:1001", "age"));
    }
}
